package com.yezi.lampcourt.http;

import android.support.annotation.NonNull;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by HongMing Ye
 * on 2017/9/19 0019.
 */

public class HttpFactory {
    private static final String SERVER_URL = "http://www.antbb.com:8080/sl2/app/";

    private static final long TIMEOUT = 30;
    private static Retrofit sRetrofit;
    private static Converter.Factory mGsonConFactory = GsonConverterFactory.create();
    private static CallAdapter.Factory mRxJavaCallAdapter = RxJava2CallAdapterFactory.create();
    private static OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request.Builder builder = chain.request().newBuilder();
                    builder.addHeader("token", "123");
                    return chain.proceed(builder.build());
                }
            })
            .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(@NonNull String message) {
                    Log.e("HttpInterceptor", message);
                }
            }).setLevel(HttpLoggingInterceptor.Level.BASIC))
            .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(TIMEOUT, TimeUnit.SECONDS)
            .build();


    public static <T> T createNormalService(Class<T> classz) {
        if (sRetrofit == null) {
            synchronized (HttpFactory.class) {
                Retrofit.Builder builder = new Retrofit.Builder();
                sRetrofit = builder.client(mOkHttpClient)
                        .baseUrl(SERVER_URL)
                        .addConverterFactory(mGsonConFactory)
                        .addCallAdapterFactory(mRxJavaCallAdapter)
                        .build();
            }
        }
        return sRetrofit.create(classz);
    }

    public static <T> T createTestService(Class<T> tClass) {
        return new Retrofit.Builder()
                .client(mOkHttpClient)
                .baseUrl("http://119.29.238.157/api/")
                .addConverterFactory(mGsonConFactory)
                .addCallAdapterFactory(mRxJavaCallAdapter)
                .build()
                .create(tClass);
    }

    public static <T> T createWeatherService(Class<T> tClass) {
        return new Retrofit.Builder()
                .client(mOkHttpClient)
                .baseUrl("http://wthrcdn.etouch.cn/")
                .addConverterFactory(mGsonConFactory)
                .addCallAdapterFactory(mRxJavaCallAdapter)
                .build()
                .create(tClass);
    }

}
